package za.um.td.entity.manager;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.entity.Entity;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;

import za.um.td.entity.Placable;
import za.um.td.entity.level.AbstractTerrain;
import za.um.td.entity.level.AxisGrid;
import za.um.td.entity.level.Terrain;
import za.um.td.entity.level.gui.LevelGui;
import za.um.td.entity.mouse.Placer;
import za.um.td.prototyping.Board;
import za.um.td.prototyping.UberTower;
import za.um.td.state.BasicGameStateSingleton;

public class LevelEntityManager {

	private static final Logger LOG = Logger.getLogger(LevelEntityManager.class
			.getName());

	ArrayList<Entity> entities;

	private static LevelEntityManager instance = null;

	private Node entityManagerNode;

	// Entities
	private AbstractTerrain terrain;
	private Placer placer = null;
	private LevelGui gui;

	private LevelEntityManager() {

		entities = new ArrayList<Entity>();

		terrain = new Terrain("Terrain", new Vector3f(0, 0, 0), new Vector3f(3,
				3, 3), 1);

		LOG.log(Level.INFO, "getXLength=" + terrain.getXLength());
		LOG.log(Level.INFO, "getZLength=" + terrain.getZLength());
		LOG.log(Level.INFO, "getNumBlocksX=" + terrain.getNumBlocksX());
		LOG.log(Level.INFO, "getNumBlocksZ=" + terrain.getNumBlocksZ());
		LOG.log(Level.INFO, "getCentreLocation=" + terrain.getCentreLocation());

		/*
		 * terrain = new Board("Terrain Board", Vector3f.ZERO, 29, 29, 1, 1, 0,
		 * ColorRGBA.white, 1);
		 */

		entities.add(terrain);

		gui = new LevelGui("Level GUI", terrain);
		entities.add(gui);

		/*if (BasicGameStateSingleton.MODE_DEBUG) {
			AxisGrid axis = new AxisGrid("Axis", new Vector3f(terrain
					.getXLength(),
					(terrain.getXLength() + terrain.getZLength()) / 2, terrain
							.getZLength()), Vector3f.UNIT_XYZ, terrain
					.getNumBlocksX()
					* (int) terrain.getScale().x);
			entities.add(axis);
		}*/

		entityManagerNode = new Node();
		
		addAllEntitySpatialsToNode();
	}

	public static LevelEntityManager createInstance() {
		if (instance == null) {
			instance = new LevelEntityManager();
		}

		return instance;
	}

	public static LevelEntityManager getInstance() {
		return instance;
	}

	public AbstractTerrain getTerrain() {
		return terrain;
	}

	public void addEntity(Entity entity) {
		if (entity != null) {
			entityManagerNode.attachChild(entity.getSpatial());
			entities.add(entity);
		}
	}

	public void removeEntity(Entity entity) {
		if (entity != null) {
			entityManagerNode.detachChild(entity.getSpatial());
			entities.remove(entity);
		}
	}

	private void addAllEntitySpatialsToNode(Node node) {
		for (Entity ent : entities) {
			node.attachChild(ent.getSpatial());
		}
	}

	private void addAllEntitySpatialsToNode() {
		addAllEntitySpatialsToNode(entityManagerNode);
	}

	public Node getEntityManagerNode() {
		return entityManagerNode;
	}

	public Placer getPlacer() {
		return placer;
	}

	public void setPlacer(Placer placer) {
		removeEntity(this.placer);
		this.placer = placer;
		addEntity(placer);
	}

}
